import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {MessageBushService} from "../../../../services/common/message-bush.service";
import {DateService} from '../../../../services/common/date.service';
import {TalentPoolProvider} from "../../../../providers/add-staff/talent-pool/talent-pool";
import * as moment from "moment";

import {ToastService} from '../../../../services/common/toast.service';

@IonicPage()
@Component({
    selector: 'page-talent-filter',
    templateUrl: 'talent-filter.html',
    providers: [
        TalentPoolProvider,
        DateService
    ]
})
export class TalentFilterPage {

    conditions = {
        origins: [
            {
                title: "线上活动"
            },
            {
                title: "系统导入"
            },
            {
                title: "手工添加"
            }
        ],
        starttime: null,
        endtime: null,
        processStartTime:null,
        processEndTime:null,
        process: []
    };

    // 时间插件--年月日汉字填充
    private pickerOptions = {
        cssClass: 'filter-add-picker'
    };

    // 年度列表
    public yearList: any[] = [];
    // 建档
    public normalErr:boolean;
    // 统计
    public processErr:boolean;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public provider: TalentPoolProvider,
                public dateService: DateService,
                public toastService:ToastService
                ) {
        this.conditions.process = this.provider.processes;
        this.yearList = this.dateService.getYearList(1900);
    }

    ionViewDidLoad() {
        let source = this.navParams.get("source");
        let starttime = this.navParams.get("starttime");
        let endtime = this.navParams.get("endtime");
        let processStartTime = this.navParams.get("processStartTime");
        let processEndTime = this.navParams.get("processEndTime");
        let process = this.navParams.get("process");

        if (source) {
            source = source.split(',');
            this.conditions.origins.forEach((i: any) => {
                let found = false;
                source.forEach(j => {
                    if (j === i.title) {
                        found = true;
                    }
                });
                i.selected = found;
            });
        }
        if (starttime) {
            this.conditions.starttime = starttime.toISOString();
        }
        if (endtime) {
            this.conditions.endtime = endtime.toISOString();
        }
        if(processStartTime){
            this.conditions.processStartTime = processStartTime.toISOString();
        }
        if(processEndTime){
            this.conditions.processEndTime = processEndTime.toISOString();
        }
        if (process) {
            process = process.split(',');
            process.forEach(i => {
                let n = Number(i);
                if (!isNaN(n)) {
                    this.conditions.process[n]['selected'] = true;
                }
            });
        }
    }

    //来源点击
    originClick(data) {
        data.selected = !data.selected;
    }

    //跟踪进度点击
    processClick(data) {
        data.selected = !data.selected;
    }

    // 清除时间
    clearTime(flag){
        switch (flag){
            case 'normal':
                this.conditions.starttime = null;
                this.conditions.endtime = null;
                break;
            case 'process':
                this.conditions.processStartTime = null;
                this.conditions.processEndTime = null;
        }

    }

    //重置
    reset() {
        this.conditions.origins.forEach((o: any) => {
            o.selected = false;
        });
        this.conditions.process.forEach((o: any) => {
            o.selected = false;
        });
        this.conditions.starttime = null;
        this.conditions.endtime = null;
        this.conditions.processStartTime = null;
        this.conditions.processEndTime = null;
    }

    // 建档时间
    normalChange(){
        if(moment(this.conditions.starttime).unix()>moment(this.conditions.endtime).unix()){
            this.normalErr=true;
            this.toastService.showToast('建档时间输入有误');
        }else{
            this.normalErr=false;
        }
    }

    // 统计
    processChange(){
        if(moment(this.conditions.processStartTime).unix()>moment(this.conditions.processEndTime).unix()){
            this.processErr=true;
            this.toastService.showToast('统计时间输入有误');
        }else{
            this.processErr=false;
        }
    }

    //完成
    finish() {
        let conditions = JSON.stringify(this.conditions);
        if(this.normalErr){
            this.toastService.showToast('建档时间输入有误');
            return false;
        }
        if(this.processErr){
            this.toastService.showToast('统计时间输入有误');
            return false;
        }
        MessageBushService.setMessage(MessageBushService.keys.talentFilterCondition, conditions);
        this.navCtrl.pop();
    }

}

